﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Drawing;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Net;
using System.IO;

public partial class Overzicht : System.Web.UI.Page
{
    public IQueryable overzicht;
    DataClassesDataContext db = new DataClassesDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {        
        if (Request.IsAuthenticated)
        {
            
            MembershipUser currentUser;
            currentUser = Membership.GetUser();
            int klantid = int.Parse(currentUser.ProviderUserKey.ToString());

            var overzicht =
            from ww in db.Winkelwagens
            join wg in db.Winkelwagengames on ww.WINKELWAGEN_ID equals wg.WINKELWAGEN_ID
            join g in db.Games on wg.GAME_NR equals g.GAME_NR
            where ww.KLANT_ID == klantid
            select new { ww.WINKELWAGEN_ID, wg.GAME_NR, wg.DATETIMEDOWNLOADAFBEELDING, wg.DATETIMEDOWNLOADGAME, ww.DATUM, wg.GAME_BOOL, wg.AFBEELDING_BOOL, g.TITEL, g.BESTANDSGROOTTEGAME, g.IMAGE };
            grid_overzicht.DataSource = overzicht;
            grid_overzicht.DataBind();


            for (int i = 0; i < grid_overzicht.Rows.Count; i++)
            {

                GridViewRow selRow = grid_overzicht.Rows[i];
                TableCell datum = selRow.Cells[4];
                DateTime aankoopdatum = Convert.ToDateTime(datum.Text);
                aankoopdatum = aankoopdatum.AddDays(1);
                if (aankoopdatum > DateTime.Now)
                {
                    foreach (var iets in overzicht)
                    {
                        if (iets.WINKELWAGEN_ID.ToString() == selRow.Cells[0].Text)
                        {
                            if (iets.GAME_NR.ToString() == selRow.Cells[1].Text)
                            {
                                if (iets.AFBEELDING_BOOL == 0 || iets.AFBEELDING_BOOL == null)
                                {
                                    TableCell knop = selRow.Cells[7];
                                    knop.Enabled = false;
                                    knop.Text = "Niet gekocht";
                                }
                                else
                                {
                                    if (iets.DATETIMEDOWNLOADAFBEELDING != null)
                                    {
                                        TableCell knop = selRow.Cells[7];
                                        knop.Enabled = false;
                                        knop.Text = "Al gedownload";
                                    }
                                }
                                if (iets.GAME_BOOL == 0 || iets.GAME_BOOL == null)
                                {
                                    TableCell knop = selRow.Cells[6];
                                    knop.Enabled = false;
                                    knop.Text = "Niet gekocht";
                                }
                                else
                                {
                                    if (iets.DATETIMEDOWNLOADGAME != null)
                                    {
                                        TableCell knop = selRow.Cells[6];
                                        knop.Enabled = false;
                                        knop.Text = "Al gedownload";
                                    }
                                }
                            }
                        }
                    }
                    //niet.Text = "download maar";
                }
                else
                {
                    TableCell knop = selRow.Cells[6];
                    knop.Enabled = false;
                    knop.Text = "Verlopen";
                    TableCell knop2 = selRow.Cells[7];
                    knop2.Enabled = false;
                    knop2.Text = "Verlopen";
                }
            }
        }
        else
        {
            grid_overzicht.Visible = false;
            niet.Text = "U bent niet ingelogd";
        }
        
    }
    public void rowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "game")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow selRow = grid_overzicht.Rows[index];
            int wwagenid = int.Parse(selRow.Cells[0].Text);
            int gamenr = int.Parse(selRow.Cells[1].Text);
            var winkelwagen = from w in db.Winkelwagengames
                              where w.GAME_NR == gamenr && w.WINKELWAGEN_ID == wwagenid
                              select w;
            Winkelwagengame ww = winkelwagen.First();

            if (ww.DATETIMEDOWNLOADGAME == null)
            {
                try
                {
                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("content-disposition", "attachment; filename=game.zip");
                    FileStream sourceFile = new FileStream(@"D:game.zip", FileMode.Open, FileAccess.ReadWrite, FileShare.None);
                    if (sourceFile.CanRead)
                    {
                        long FileSize;
                        FileSize = sourceFile.Length;
                        byte[] getContent = new byte[(int)FileSize];
                        sourceFile.Read(getContent, 0, (int)sourceFile.Length);
                        sourceFile.Close();
                        Response.BinaryWrite(getContent);
                    }

                }
                catch (Exception exc)
                {
                    Console.WriteLine("Bestandsfout" + exc.Message);
                }

                ww.DATETIMEDOWNLOADGAME = DateTime.Now;
                db.SubmitChanges();
            }
            else
            {
                niet.Text = "Hij is al gedownload!";
            }

           
        }
        else if (e.CommandName == "cover")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow selRow = grid_overzicht.Rows[index];
            int wwagenid = int.Parse(selRow.Cells[0].Text);
            int gamenr = int.Parse(selRow.Cells[1].Text);
            var winkelwagen = from w in db.Winkelwagengames
                              where w.GAME_NR == gamenr && w.WINKELWAGEN_ID == wwagenid
                              select w;
            Winkelwagengame ww = winkelwagen.First();
            string url;
            if (ww.DATETIMEDOWNLOADAFBEELDING == null)
            {
                try
                {

                    url = "pes2010.jpg";
                    System.IO.FileInfo file = new System.IO.FileInfo(url);
                    if (file.Exists)
                    {
                        // clear the current output content from the buffer 
                        Response.Clear();
                        // add the header that specifies the default filename for the Download/SaveAs dialog 
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                        // add the header that specifies the file size, so that the browser can show the download progress 
                        Response.AddHeader("Content-Length", file.Length.ToString()); // specify that the response is a stream that cannot be read by the client and must be downloaded 
                        Response.ContentType = "application/octet-stream";
                        // send the file stream to the client 
                        Response.WriteFile(file.FullName);

                        ww.DATETIMEDOWNLOADAFBEELDING = DateTime.Now;
                        db.SubmitChanges();

                    }

                }
                catch (Exception exc)
                {
                    Console.WriteLine("Bestandsfout" + exc.Message);
                }
            }
            else
            {
                niet.Text = "Hij is al gedownload!";
            }
        }
    }
}
